part of _;

extension on MainPage {
  Widget getTabBarIcon(String image) => Container(margin: EdgeInsets.fromLTRB(0, 0, 0, 2), child: Image.asset("images/tabbar/$image"), width: 22, height: 22);

  Widget view_body() {
    return Stack(
      children: [
        PageView.builder(
          physics: globalUserModel == null ? NeverScrollableScrollPhysics() : ClampingScrollPhysics(),
          onPageChanged: (index) async {
            if (await AppUtils.checkIsLogon(yPage, goLogin: true)) {
              currentIndex = index;
              controller_reloadHome();
            } else {
              pageController.jumpToPage(0);
            }
            mainPage_video.videoPagerController?.pause();
            controller_reloadHome();
          },
          controller: pageController,
          itemCount: pages.length,
          itemBuilder: (context, index) => pages[index],
        ),
        globalUserModel != null
            ? YEmpty()
            : Positioned(
                bottom: 0,
                child: view_login_bar(),
              ),
      ],
    );
  }

  Widget view_bottom() {
    return Container(
      height: ySafeAreaBottomHeight() + navBarHeight,
      decoration: BoxDecoration(
        boxShadow: [
          BoxShadow(
            blurRadius: 10, //阴影范围
            spreadRadius: 0, //阴影浓度
            color: Colors.black.withAlpha(50), //阴影颜色
          ),
        ],
      ),
      child: ClipRRect(
        // borderRadius: BorderRadius.only(topRight: Radius.circular(20), topLeft: Radius.circular(20)),
        child: BottomTabBar(
          textColor: Colors.black,
          type: BottomTabBarType.fixed,
          fixedColor: YConfig.themeColor,
          isAnimation: true,
          bgColor: currentIndex == 1 ? Colors.grey.shade300 : Colors.white,
          currentIndex: currentIndex,
          onTap: (index) async {
            if (await AppUtils.checkIsLogon(yPage, goLogin: true)) pageController.jumpToPage(index);
          },
          items: [
            BottomTabBarItem(title: Text("首页", overflow: TextOverflow.visible), icon: getTabBarIcon("tabbar_shouye_unselect.png"), activeIcon: getTabBarIcon("tabbar_shouye_select.png")),
            BottomTabBarItem(title: Text("视频", overflow: TextOverflow.visible), icon: getTabBarIcon("tabbar_shipin_unselect.png"), activeIcon: getTabBarIcon("tabbar_shipin_select.png")),
            BottomTabBarItem(title: Text("消息", overflow: TextOverflow.visible), icon: getTabBarIcon("tabbar_xiaoxi_unselect.png"), activeIcon: getTabBarIcon("tabbar_xiaoxi_select.png"), badge: view_badge_xiaoxi()),
            BottomTabBarItem(title: Text("购物车", overflow: TextOverflow.visible), icon: getTabBarIcon("tabbar_gouwuche_unselect.png"), activeIcon: getTabBarIcon("tabbar_gouwuche_select.png")),
            BottomTabBarItem(title: Text("我的", overflow: TextOverflow.visible), icon: getTabBarIcon("tabbar_wode_unselect.png"), activeIcon: getTabBarIcon("tabbar_wode_select.png")),
          ],
        ),
      ),
    );
  }

  Widget view_login_bar() {
    return Container(
      width: yScreenSize().width,
      padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
      color: Colors.black.withAlpha(200),
      child: Row(
        children: [
          Expanded(child: Text("登录${YConfig.appName}打开精彩世界", style: TextStyle(fontSize: 16, color: Colors.white, fontWeight: FontWeight.bold))),
          YButton(
            width: 100,
            height: 35,
            borderRadius: BorderRadius.circular(99),
            decoration: BoxDecoration(gradient: IFinal.Colors.btnThemeGradient),
            child: Text("立即登录", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
            onTap: () => AppUtils.checkIsLogon(yPage, goLogin: true),
          ),
        ],
      ),
    );
  }

  Widget view_badge_xiaoxi() {
    return Draggable(
      feedback: Material(color: Colors.transparent, child: MyViews.view_badge(badge_message_count)),
      child: Visibility(
        visible: !badge_message_isDraging,
        child: MyViews.view_badge(badge_message_count),
      ),
      onDragStarted: () => setState(() => badge_message_isDraging = true),
      onDragEnd: (details) {
        badge_message_isDraging = false;
        if (details.offset.dy < yScreenSize().height - (ySafeAreaBottomHeight() + navBarHeight + 20)) {
          final overlay = OverlayEntry(
            builder: (context) {
              return Positioned(
                left: details.offset.dx,
                top: details.offset.dy,
                child: Image.asset("images/badge_dismiss_mask.gif", width: 30, height: 30),
              );
            },
          );
          Overlay.of(context).insert(overlay);
          yRun(() => setState(() => overlay.remove()), delay: 500);
          YIM.conversationManager.clearAllUnreadCount();
          badge_message_count = 0;
        }
        setState(() {});
      },
    );
  }
}
